import { CloseCircleFilled, QuestionCircleFilled } from '@ant-design/icons-vue';
import { Modal } from 'ant-design-vue';
import { defineComponent, h, createVNode, VNode } from 'vue';

let modal;

const error = (title: string, content: string | VNode, onOk: () => void = () => {}, onCancel: () => void = () => {}) => {
  modal = Modal.error({
    title,
    content,
    icon: createVNode(CloseCircleFilled),
    onOk,
    onCancel,
    okText: '确定',
    getContainer() {
      return document.body;
    },
  });
};

const confirm = (title: string, content: string, onOk: () => void = () => {}, onCancel: () => void = () => {}) => {
  modal = Modal.confirm({
    title,
    content,
    icon: createVNode(QuestionCircleFilled),
    okText: '确定',
    cancelText: '取消',
    onOk,
    onCancel,
    getContainer() {
      return document.body;
    },
  });
};

const close = () => {
  modal?.destroy();
};

/**
 *
 * @returns
 * @method error(title,content,onOk?,onCancel?) 错误信息弹出
 */
export const useModalAlert = () => {
  return {
    error,
    confirm,
    close,
  };
};
